package com.quarters.controller.admin;


import com.alibaba.druid.mock.MockArray;
import com.quarters.bean.Article;
import com.quarters.bean.ArticleOrder;
import com.quarters.bean.RoleQuarters;
import com.quarters.bean.UserInfo;
import com.quarters.bean.layuiBean.ResultPage;
import com.quarters.bean.layuiBean.VoPage;
import com.quarters.commen.BaseController;
import com.quarters.util.Respons.ResponseUtil;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 文章和订单表 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2019-09-04
 */
@Controller
@RequestMapping("admin/articleOrder")
public class ArticleOrderController extends BaseController {


    /**
     * @return 跳转审核
     */
    @RequestMapping(value = "success.html", method = RequestMethod.GET)
    public String examine(Model model, Long id) {
        ArticleOrder articleOrder = articleOrderService.getById(id);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        String currentDate = dateFormat.format(new Date());
        List<ArticleOrder> articleOrdersList = articleOrderService.selectByIdTime(articleOrder.getUserId(), currentDate);
        model.addAttribute("order", articleOrder);
        String content = articleOrder.getContent();
        model.addAttribute("content", delHtmlTag(content));
        return "admin/examine/successPay";
    }

    /**
     * @param str
     * @return 去除html标签
     */
    public static String delHtmlTag(String str) {
        String newstr = "";
        newstr = str.replaceAll("<[.[^>]]*>", "");
        newstr = newstr.replaceAll(" ", "");
        return newstr;
    }

    /**
     * @param voPage
     * @return 后台查询
     */
    @RequestMapping(value = "selectMyPage", method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<ArticleOrder> selectMyPage(VoPage voPage, HttpSession session) {
        if (getUserInfo(session).getUsername().equals("admin")) {
            return articleOrderService.selectMyPageAll(voPage);

        } else {
            return articleOrderService.selectMyPage(voPage, getUserInfo(session).getId());
        }
    }

    /**
     * @param articleOrder
     * @return 通过
     */
    @RequestMapping(value = "success", method = RequestMethod.POST)
    @ResponseBody
    public Map success(@RequestBody ArticleOrder articleOrder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        String currentDate = dateFormat.format(new Date());
        List<ArticleOrder> articleOrdersList = articleOrderService.selectByIdTime(articleOrder.getUserId(), currentDate);

//        if(articleOrdersList.size()>2){
        articleOrder.setTypeType(1);
        articleOrder.setType(2);
        articleOrder.setAuditTime(System.currentTimeMillis());
        articleOrder.setOrderMoeny(articleOrder.getOrderMoeny());
        UserInfo userInfo = userInfoService.getById(articleOrder.getUserId());
        articleOrder.setOpenid(userInfo.getOpenId());
        if (articleOrderService.updateById(articleOrder)) {

            Article article = articleService.getById(articleOrder.getArticleId());
            article.setType(1);
            if (articleService.updateById(article)) {
                articleService.pay(articleOrder.getUserId(), System.currentTimeMillis(), articleOrder.getTitle(), articleOrder.getOrderMoeny(), articleOrder.getOrderNumber(), 1);
                return ResponseUtil.getSuccessMap();
            } else {
                return ResponseUtil.getExceptionMap();
            }
        } else {
            return ResponseUtil.getExceptionMap();
        }
    }


    /**
     * @param articleOrder
     * @return 不通过
     */
    @RequestMapping(value = "error", method = RequestMethod.POST)
    @ResponseBody
    public Map error(@RequestBody ArticleOrder articleOrder) {
        articleOrder.setTypeType(2);
        articleOrder.setType(2);
        articleOrder.setAuditTime(System.currentTimeMillis());
        if (articleOrderService.updateById(articleOrder)) {
            Article article = articleService.getById(articleOrder.getArticleId());
            article.setType(2);
            if (articleService.updateById(article)) {
                //小于两次通过之后就直接推送给用户
                articleOrderService.error(articleOrder.getUserId(), System.currentTimeMillis(), articleOrder.getTitle(), 1);
                return ResponseUtil.getSuccessMap();
            } else {
                return ResponseUtil.getExceptionMap();
            }
        }
        return ResponseUtil.getExceptionMap();
    }


    /**
     * @param voPage
     * @return 订单查询
     */
    @RequestMapping(value = "articleOrder", method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<ArticleOrder> articleOrder(VoPage voPage, HttpSession session,String time) {
        if (getUserInfo(session).getUsername().equals("admin")) {
            return articleOrderService.selectOrderAll(voPage,time);
        }else{
            List<RoleQuarters> roleQuarters = roleQuartersService.selectFlage(getUserInfo(session).getId());
            for (int i = 0; i < roleQuarters.size(); i++) {
                if(roleQuarters.get(i).getFlage()==1){
                    return articleOrderService.selectOrder(voPage,getUserInfo(session).getId(),time);
                }
            }
            return articleOrderService.selectLableOrder(voPage,getUserInfo(session).getId(),time);
        }
    }


}
